import sys
import os
import string
import re

from py2neo import Graph, Path, Node, Relationship,NodeSelector


#import all user id form txt file after distinct
def user_id_import(file_name):
    print "user id import start!"
    fp = open(file_name,"r")
    lines = fp.readlines()
    line_list = []
    for line in lines:
        #print line
        line_list.extend(line.split())
        #print "line count:"+ len(line_list)
    fp.close()
    print "primary id count:",
    print len(line_list)
    unique_str = list(set(line_list))
    unique_uid = []
    for uid in unique_str:
        unique_uid.append(int(uid))
    print "unique_uid:",
    print len(unique_uid)
    
    return unique_uid
#create local node
def create_node_map(graph,label_name,uid_list):
    uid_map = {}
    for uid in uid_list:
        #print line
        uid_map[uid] = Node(label_name,uid=uid,name=str(uid))
    print "uid_map len:",
    print len(uid_map)
    return uid_map
    
user_total_count = 0 
#check if node has beed created in db
def check_node(graph,label_name,uid):
    selector = NodeSelector(graph)
    selected = selector.select(label_name, name=uid)
    node_list = list(selected)
    node_len = len(node_list)
    if node_len > 1:#error
        print "node repeat in database!"
        return node_list[0]
    elif node_len == 1:#yes
        return node_list[0]
    else:#no
        global user_total_count
        user_total_count = user_total_count+1
        uid_int = int(uid)
        uid_str = str(uid)
        #print uid_str
        #print uid_int
        #print "create node for uid %d" %uid_int
        user_node = Node(label_name, name=uid_str,uid=uid_int)
        graph.create(user_node)
        return user_node
#create node and relationship
def create_node_and_relationship(node_map,graph,label_name,uid_list):
    uid_len = len(uid_list)
    if uid_len >= 1 : 
        tx = graph.begin()
        user_id = int(uid_list[0]) 
        user_node = check_node(graph,label_name,user_id)
        for uid in uid_list[1:]:
            follow_node = check_node(graph,label_name,uid)
            #print follow_node
            #print user_node
            relation = Relationship(follow_node, "FOLLOWS",user_node)
            tx.create(relation)
        tx.commit()
        print "deal total %s follows for user:%s" %((uid_len-1),user_id)
        
if __name__ == '__main__':
    print "dao data start!"
    label_name = sys.argv[1]
    file_name = sys.argv[2]
    print "label_name:"+label_name
    print "file_name:"+file_name
    command = raw_input("continue?:[y/n]")
    if command == "y" or command == "Y" :
        #secure_graph = Graph(host="125.39.193.243",bolt_port=7687,user="neo4j",password="test123456")
        secure_graph = Graph(host="10.13.1.48",bolt_port=7687,user="neo4j",password="test123456")
        #clear all data
        print "clear all data in label "+label_name
        secure_graph.run("match (n:"+label_name+") optional match (n)-[r]->() delete n,r")
        
        #import all id
        uid_list = user_id_import(file_name)
        #create all node
        node_map = create_node_map(secure_graph,label_name,uid_list)
        #load each user's follows
        #create all relatrionship
        tx = secure_graph.begin()
        
        print "user links import start!"
        fp = open(file_name,"r")
        lines = fp.readlines()
        percent_user = (len(lines)/100) + 1
        for user_node in node_map:
            tx.create(node_map[user_node])
        print "create node done!"

        links_list = []
        ii=0
        for line in lines:
            ii = ii+1
            uid_list = line.split()
            user_id = int(uid_list[0])
            follow_node =node_map[user_id] 
            for uid in uid_list[1:]:
                uid_int = int(uid)
                user_node = node_map[uid_int] 
                relation = Relationship(follow_node, "FOLLOWS",user_node)
                tx.create(relation)
            if (ii % percent_user) ==  (percent_user-1):
                print "current user num:%s deal with %%%s" %(ii ,ii / percent_user)
        fp.close()
        print "deal with 100%"
        #print "total user:%d" %(user_total_count)
        print "commit start:"
        tx.commit()
        print "dao data done!"
    else:
        print "quit dao data"

